package top.hkyzf.util;

import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.util.thread.NamedThreadFactory;
import net.minecraftforge.client.ClientCommandHandler;

import java.util.Random;
import java.util.concurrent.Executor;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * description: 工具类
 * @author zhufeng
 * @date 2022/8/4 17:24
 * @version V1.0
 */
public class Utils {

    /**
     * 当前 MOD 的唯一标识
     */
    public static final String MOD_ID = "onekeycommand";
    /**
     * 执行工作的线程池
     */
    private static final ThreadPoolExecutor GAME_THREAD_POOL =
            new ThreadPoolExecutor(5, 10, 60L, TimeUnit.SECONDS,
            new SynchronousQueue<>(),
            new NamedThreadFactory("OneKeyCommand"),
            new ThreadPoolExecutor.CallerRunsPolicy());

    /**
     * 使用新的线程执行工作
     * @return 线程池
     */
    public static Executor getExecutor() {
        return GAME_THREAD_POOL;
    }

    /**
     * 封装游戏中发送聊天框消息的方法，方便调试
     * @param message 要显示的消息
     */
    public static void sendMessage(String message) {
        assert Minecraft.getInstance().player != null;
        Minecraft.getInstance().player.sendMessage(
                new TextComponent(message),
                Minecraft.getInstance().player.getUUID());
    }

    /**
     * 封装游戏中发送命令的方法
     * @param command 要发送的命令
     */
    public static void sendCommand(String command) {
        ClientCommandHandler.sendMessage(command);
    }

    /**
     * 获取一个 [min, max] 范围内的随机数
     * @param min 最小值
     * @param max 最大值
     * @return 随机数
     */
    public static int getRandom(int min, int max) {
        Random random = new Random();
        return random.nextInt(max)%(max-min+1) + min;
    }
}

